/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

var osapps = osapps || {};

osapps.loadJavaScript = function(filename) {
  document.write("<script type='text/javascript' src='" + filename + "'></" + "script>");
};
osapps.loadCss = function(filename) {
  document.write("<link type='text/css' rel='stylesheet' href='" + filename + "'></link>");
};

osapps.Loader = {
  DEFAULT_DOMAIN : "DEFAULT",
  DEFAULT_DOMAIN_ID : "DEFAULT",
  
  deployInfo: {
    domain : "DEFAULT",
    container_id : "DEFAULT",
    frontend_url: null,
    backend_url: null
  },
  
  dynamicLoading : {
    DEFAULT : {
      JS : [],
      CSS : []
    }
  },
  
  readDomainInfo : function () {
    if (opensocial && opensocial.getEnvironment) {
      var environment = opensocial.getEnvironment();
      if (environment && environment.getDomain) {
        this.deployInfo.domain = environment.getDomain();
        this.deployInfo.container_id = osapps.Containers.domainToId[this.deployInfo.domain] || "DEFAULT";
      }
    }
  },
  
  setFrontend : function(server_url) {
    if(!server_url || server_url.trim().length == 0) {
      alert("Initialize App failed: frontend_url cannot be empty.");
      return;
    }
    server_url = server_url.trim();
    var lastChar = server_url.charAt(server_url.length - 1);
    if(lastChar != "/" && lastChar != "\\") {
      server_url += "/";
    }
    this.deployInfo.frontend_url = server_url;
  },
  setBackend : function(server_url) {
    if(!server_url || server_url.trim().length == 0) {
      alert("Initialize App failed: backend_url cannot be empty.");
      return;
    }
    server_url = server_url.trim();
    var lastChar = server_url.charAt(server_url.length - 1);
    if(lastChar != "/" && lastChar != "\\") {
      server_url += "/";
    }
    this.deployInfo.backend_url = server_url;
  }, 
  
  
  loadDynamicJsCss : function (urlPath) {
    if(!urlPath || urlPath.trim().length == 0) {
      urlPath = this.deployInfo.frontend_url;
    } else {
      urlPath = urlPath.trim();
      var lastChar = urlPath.charAt(urlPath.length - 1);
      if(lastChar != "/" && lastChar != "\\") {
        urlPath += "/";
      }
    }
    if (!this.deployInfo.container_id 
        || !(this.deployInfo.container_id in this.dynamicLoading)) {
      this.deployInfo.container_id = this.DEFAULT_DOMAIN_ID;
    }
    var configuration = this.dynamicLoading[this.deployInfo.container_id]; 
    if (configuration && configuration.JS && configuration.JS.length) {
      for(var i = 0; i < configuration.JS.length; i ++) {
        var jsFile = configuration.JS[i];
        if(jsFile && jsFile.trim().length) {
          osapps.loadJavaScript(urlPath + jsFile);
        }
      }
    }
    if (configuration && configuration.CSS && configuration.CSS.length) {
      for(var i = 0; i < configuration.CSS.length; i ++) {
        var cssFile = configuration.CSS[i];
        if(cssFile && cssFile.trim().length) {
          osapps.loadCss(urlPath + cssFile);
        }
      }
    }
  }
};
